package com.wx.disparent.mallparent.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.disparent.mallparent.commons.util.PageResult;
import com.wx.disparent.mallparent.entity.MerchantEntity;
import com.wx.disparent.mallparent.entity.ParaEntity;
import com.wx.disparent.mallparent.entity.ProductEntity;
import com.wx.disparent.mallparent.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商家控制层
 * @author 刘杰龙
 * @version 1.0
 * @date 2019/12/10 10:28
 */
@RestController
@RequestMapping("/admin/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 获取所有商家信息
     * @return 商家集合
     */
    @RequestMapping("/getList")
    public ModelAndView getList(){
        ModelAndView model = new ModelAndView();
        model.setViewName("web/pc/merchantListPage");
        return model;
    }

    /**
     * 获取所有商家信息 （分页查询）
     *
     * @return 商品集合
     */
    @RequestMapping("/pageTable")
    public String pageTable(@RequestParam("aoData") String aoData) {
        PageResult pr = new PageResult(aoData);
        pr.setOrderBy("id");
        String search = pr.getSearch();

        JSONArray jsonArray = new JSONArray();

        List<MerchantEntity> entities;
        if (search.equals("")) {
            entities = merchantService.findAll(null);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("name", search);
            entities = merchantService.findAll(map);
        }

        for (Object obj : entities) {
            MerchantEntity entity = (MerchantEntity) obj;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("merchantId", entity.getId());
            jsonObject.put("merchantName", entity.getName());
            jsonObject.put("merchantStatus", entity.getIsDel());
            jsonObject.put("merchantOperate", entity.getIsDel());
            jsonArray.add(jsonObject);
        }

        String json = "{\"sEcho\":" + (pr.getsEcho()+1) + ",\"iTotalRecords\":" + pr.getPage().getTotal() + ",\"iTotalDisplayRecords\":" + pr.getPage().getTotal() + ",\"aaData\":" + jsonArray.toString() + "}";

        return json;
    }

    /**
     * 修改商家状态（启用、禁用）
     * @param merchantId 商家ID
     * @param type 商家要修改的状态
     * @return 商家列表页面（更改后）
     */
    @RequestMapping("/availableOrDisabled")
    public ModelAndView sellOrSold(@RequestParam(value = "id") Integer merchantId,@RequestParam(value = "type") String type){
        if (type.equals("0")){
            merchantService.changeStatus(merchantId,"available");
        }else if (type.equals("1")){
            merchantService.changeStatus(merchantId,"disabled");
        }

        return getList();
    }

    /**
     * 获取商家信息
     * @param merchantId 商家ID
     * @return 商家信息
     */
    @RequestMapping("/merchantInfo")
    public ModelAndView merchantInfo(@RequestParam(value = "id",required = false) Integer merchantId){
        ModelAndView model = new ModelAndView();

        Map<String, Object> param = new HashMap<String,Object>();

        MerchantEntity merchantEntity = null;
        if (merchantId != null && !merchantId.equals("")){
            merchantEntity = merchantService.findById(merchantId);
        }
        param.put("paraId","bankcard_type");
        List<ParaEntity> allPara = merchantService.findAllPara(param);
        model.addObject("paras",allPara);

        model.addObject("merchant",merchantEntity);
//            model.addObject("userEntity",merchantEntity);
        model.setViewName("web/pc/merchantInfoPage");
        return model;
    }

    /**
     * 添加或修改商家
     * @return 商家信息
     */
    @RequestMapping("/addOrModify")
    public ModelAndView saveMerchant(MerchantEntity merchantEntity){
        if (merchantEntity.getId() != null){
            merchantService.update(merchantEntity);
        }else {
            merchantService.insert(merchantEntity);
        }

        return getList();
    }
}
